package com.dasel.math


import groovyx.gpars.ParallelArrayUtil
import static groovyx.gpars.Parallelizer.*

//import static VectorUtils.*
/**
 * Created by IntelliJ IDEA.
 * User: benoit
 * Date: 28-Feb-2010
 * Time: 18:12:22
 * To change this template use File | Settings | File Templates.
 */
@Typed
class Prid {
    Integer rows
    Integer cols
    double[][] matrix

    Prid plus(Prid right) {
        double[][] results = new double[rows][cols]
        withParallelizer {
            ParallelArrayUtil.eachWithIndexParallel(results, {double[] r, int i ->
                double[] a = this.matrix[i];
                double[] b = right.matrix[i];
                //double[] r = results[i];
                for (int j = 0; j < cols; j++) {
                    r[j] = a[j] + b[j]
                }
            })
        }
        return new Prid(matrix: results, rows: rows, cols: cols)
    }


    Prid plus(Number right) {
        double[][] results = new double[rows][cols];
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                results[i][j] = this.matrix[i][j] + right
            }
        }
        return new Prid(matrix: results, rows: rows, cols: cols)
    }

    Prid multiply(Prid right) {
        double[][] results = new double[rows][cols]
        withParallelizer {
            ParallelArrayUtil.eachWithIndexParallel(results, {double[] r, int i ->
                double[] a = this.matrix[i];
                double[] b = right.matrix[i];
                //double[] r = results[i];
                for (int j = 0; j < cols; j++) {
                    r[j] = a[j] * b[j]
                }
            })
        }
        return new Prid(matrix: results, rows: rows, cols: cols)
    }


    Prid multiply(Number right) {
        double[][] results = new double[rows][cols];
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                results[i][j] = this.matrix[i][j] * right
            }
        }
        return new Prid(matrix: results, rows: rows, cols: cols)
    }
}